package com.syblood.app.camera;

import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.GLSurfaceView;
import android.util.AttributeSet;
import android.util.Log;

import com.syblood.app.camera.callback.ICameraGLSurfaceViewCallback;
import com.syblood.app.camera.callback.ICameraSurfaceRenderCallback;
import com.syblood.app.camera.watermark.render.CameraSurfaceRender;
import com.syblood.app.camera.watermark.render.RenderDrawerGroups;
import com.syblood.app.utils.ConstantsUtils;


/**
 * Created By Chengjunsen on 2018/8/25
 */
public class DoubleCameraGLSurfaceView extends GLSurfaceView implements ICameraSurfaceRenderCallback {
    private final static String TAG = ConstantsUtils.TAG_HEAD + DoubleCameraGLSurfaceView.class.getSimpleName();
    private int mRatioWidth = 0;
    private int mRatioHeight = 0;
    private CameraSurfaceRender mRender;
    private ICameraGLSurfaceViewCallback mCallback;


    public DoubleCameraGLSurfaceView(Context context) {
        super(context, null);
    }

    public DoubleCameraGLSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        Log.d(TAG,"CameraGLSurfaceView creat");
        init(context);

    }

    private void init(Context context) {
        Log.d(TAG,"CameraGLSurfaceView init");
        setEGLContextClientVersion(3);
        setDebugFlags(GLSurfaceView.DEBUG_CHECK_GL_ERROR);
        mRender = new CameraSurfaceRender(context);
        mRender.setCallback(this);
        this.setRenderer(mRender);
        this.setRenderMode(RENDERMODE_WHEN_DIRTY);
    }

    public SurfaceTexture getCamera1SurfaceTexture() {
        return mRender.getCamera1SurfaceTexture();
    }

    public SurfaceTexture getCamera2SurfaceTexture() {
        return mRender.getCamera2SurfaceTexture();
    }

    @Override
    public void onRequestRender() {
        requestRender();
    }

    @Override
    public void onCreate() {
        if (mCallback != null) {
            mCallback.onSurfaceViewCreate(getCamera1SurfaceTexture(), getWidth(), getHeight());
        }
    }

    @Override
    public void onChanged(int width, int height) {
        if (mCallback != null) {
            mCallback.onSurfaceViewChange(width, height);
        }
    }

    @Override
    public void onDraw() {

    }


    /**
     * Sets the aspect ratio for this view. The size of the view will be measured based on the ratio
     * calculated from the parameters. Note that the actual sizes of parameters don't matter, that
     * is, calling setAspectRatio(2, 3) and setAspectRatio(4, 6) make the same result.
     *
     * @param width  Relative horizontal size
     * @param height Relative vertical size
     */
    public void setAspectRatio(int width, int height) {
        if (width < 0 || height < 0) {
            throw new IllegalArgumentException("Size cannot be negative.");
        }
        mRatioWidth = width;
        mRatioHeight = height;
        requestLayout();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        if (0 == mRatioWidth || 0 == mRatioHeight) {
            setMeasuredDimension(width, height);
        } else {
            if (width < height * mRatioWidth / mRatioHeight) {
                setMeasuredDimension(width, width * mRatioHeight / mRatioWidth);
            } else {
                setMeasuredDimension(height * mRatioWidth / mRatioHeight, height);
            }
        }
    }

    public void setCallback(ICameraGLSurfaceViewCallback mCallback) {
        this.mCallback = mCallback;
    }

    public void releaseSurfaceTexture() {
        mRender.releaseSurfaceTexture();
    }

    public void resumeSurfaceTexture() {
        mRender.resumeSurfaceTexture();
    }

    public void startRecord() {
        mRender.startRecord();
    }

    public void stopRecord() {
        mRender.stopRecord();
    }

    public boolean isRecording() {
        return mRender.isRecording();
    }

    public void destroy() {
        mRender.destroy();
    }
}
